﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace aaaSoft.SkinEngine
{
    public partial class SkinEngine : Component
    {
        /// <summary>
        /// 保存所有控件对应的IMyControl
        /// </summary>
        Hashtable htIMyControls = new Hashtable();

        public SkinEngine()
        {
            InitializeComponent();
        }

        public SkinEngine(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
        }

        /// <summary>
        /// 是否处于设计模式
        /// </summary>
        public bool IsDesignMode
        {
            get
            {
                return this.DesignMode;
            }
        }

        #region 是否开启皮肤引擎
        private Boolean _IsUseSkin = false;
        /// <summary>
        /// 是否开启皮肤引擎
        /// </summary>
        public Boolean IsUseSkin
        {
            get
            {
                return _IsUseSkin;
            }
            set
            {
                if (_IsUseSkin == value)
                {
                    return;
                }
                _IsUseSkin = value;
                
                if (value)
                {
                    StartEngine();
                }
                else
                {
                    StopEngine();
                    if (this.DesignMode)
                    {
                        StopEngine();
                    }
                }
                ContainerControl.Refresh();
            }
        }
        #endregion

        #region 皮肤背景色
        private Color _BackColor = Color.LightSkyBlue;
        /// <summary>
        /// 皮肤背景色
        /// </summary>
        public Color BackColor
        {
            get
            {
                return _BackColor;
            }
            set
            {
                if (_BackColor == value)
                {
                    return;
                }
                _BackColor = value;
                if (ContainerControl != null)
                {
                    ContainerControl.Refresh();
                }
                BackColorChanged(this, new EventArgs());
            }
        }
        #endregion

        #region 初始化皮肤引擎
        /// <summary>
        /// 初始化皮肤引擎
        /// </summary>
        private void StartEngine()
        {
            BindEvent(ContainerControl);
            //Application.AddMessageFilter(new MyMessageFilter());
        }
        #endregion

        #region 停止皮肤引擎
        /// <summary>
        /// 停止皮肤引擎
        /// </summary>
        private void StopEngine()
        {
            UnbindEvent(ContainerControl);
            //Application.RemoveMessageFilter(new MyMessageFilter());
        }
        #endregion

        #region 递归绑定事件
        /// <summary>
        /// 递归绑定事件
        /// </summary>
        /// <param name="ctl"></param>
        private void BindEvent(Control ctl)
        {
            ctl.ControlAdded += new ControlEventHandler(Control_ControlAdded);
            ctl.ControlRemoved += new ControlEventHandler(Control_ControlRemoved);

            MyControls.IMyControl ICtl = GetIMyControl(ctl);
            //开始控件皮肤
            if (ICtl != null)
            {
                ICtl.StartControlSkin();
            }

            //递归
            foreach (Control tmpctl in ctl.Controls)
            {
                BindEvent(tmpctl);
            }
        }
        #endregion

        #region 递归取消绑定相关事件
        /// <summary>
        /// 递归取消绑定相关事件
        /// </summary>
        private void UnbindEvent(Control ctl)
        {
            ctl.ControlAdded -= new ControlEventHandler(Control_ControlAdded);
            ctl.ControlRemoved -= new ControlEventHandler(Control_ControlRemoved);

            MyControls.IMyControl ICtl = GetIMyControl(ctl);
            //停止控件皮肤
            if (ICtl != null)
            {
                ICtl.StopControlSkin();
            }

            //递归
            foreach (Control tmpctl in ctl.Controls)
            {
                UnbindEvent(tmpctl);
            }
        }
        #endregion

        #region 根据Control返回IMyControl接口
        /// <summary>
        /// 根据Control返回IMyControl接口
        /// </summary>
        /// <param name="ctl">控件</param>
        /// <returns></returns>
        private MyControls.IMyControl GetIMyControl(Control ctl)
        {
            MyControls.IMyControl ICtl = null;
            if (htIMyControls.ContainsKey(ctl))
            {
                //如果已经存在Hash表中
                ICtl = htIMyControls[ctl] as MyControls.IMyControl;
            }
            else
            {
                //如果不存在Hash表中

                if (ctl is Form)
                {
                    //如果是窗体
                    ICtl = new MyControls.MyForm(ctl as Form, this);
                }
                else if (ctl is Button)
                {
                    //如果是按钮
                    ICtl = new MyControls.MyButton(ctl as Button, this);
                }
                else if (ctl is ProgressBar)
                {
                    //如果是进度条
                    ICtl = new MyControls.MyProgressBar(ctl as ProgressBar, this);
                }
                else if (ctl is ListView)
                {
                    //如果是列表
                    ICtl = new MyControls.MyListView(ctl as ListView, this);
                }
                else if (ctl is Label)
                {
                    //如果是Label标签
                    ICtl = new MyControls.MyLabel(ctl as Label, this);
                }
                else if (ctl is GroupBox)
                {
                    //如果是GroupBox
                    ICtl = new MyControls.MyGroupBox(ctl as GroupBox, this);
                }
                else if (ctl is TabControl)
                {
                    //如果是TabControl
                    ICtl = new MyControls.MyTabControl(ctl as TabControl, this);
                }
                else if (ctl is TrackBar)
                {
                    //如果是TrackBar
                    ICtl = new MyControls.MyTrackBar(ctl as TrackBar, this);
                }
                else if (ctl is Panel)
                {
                    //如果是面板
                    ICtl = new MyControls.MyPanel(ctl as Panel, this);
                }

                //添加到Hash表中
                if (ICtl != null)
                {
                    htIMyControls.Add(ctl, ICtl);
                }
            }
            return ICtl;
        }
        #endregion

        private void Control_ControlAdded(object sender, ControlEventArgs e)
        {
            BindEvent(e.Control);
        }

        
        private void Control_ControlRemoved(object sender, ControlEventArgs e)
        {
            UnbindEvent(e.Control);
        }

        /// <summary>
        /// 皮肤引擎背景色改变事件
        /// </summary>
        public event EventHandler BackColorChanged;
    }
}
